package com.blog.service.imp;
 
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blog.common.base.BaseController;
import com.blog.common.base.ResponseReslut;
import com.blog.common.util.MD5Util;
import com.blog.dao.sys.UserDao;
import com.blog.entity.sys.User;
import com.blog.service.sys.UserService;
 

@Service
public class UserServiceImp extends BaseController  implements UserService{
	
	@Autowired
	private UserDao userDao;
	
	@Override
	public ResponseReslut saveUser(User user) {
		
		if(StringUtils.isEmpty(user.getPassword()))
		{
			return resultError("密码不能为空");
		}
		//密码加密  
		String pwdMd5=MD5Util.MD5(user.getPassword());
		user.setPassword(pwdMd5);
		user.setCreated(new Date());
		user.setUpdated(new Date());
		int row=userDao.insertUser(user);
		if(row<=0)
		{
			return resultError("注册用户失败。请联系管理员");
		}
		
		//TODO 发送邮件   
		
		return setResultSuccess("添加用户成功！",row);
	}

	@Override
	public ResponseReslut findByUserName(String username) {
		
		User user=userDao.findByUserName(username);
		if(user==null)
		{
			return resultError("用户不存在！");
		}
		return resultSuccess(user);
	}

	@Override
	public List<User> listUser(Map<String,Object> map) {
		return userDao.listUser(map);
	}

	@Override
	public Integer countUser(Map<String,Object> map) {
		return userDao.countListUser(map);
	}

	@Override
	public Integer deleteUsers(Integer userIds) {
		return userDao.deleteUsers(userIds);
	}

	@Override
	public User getUser(Integer id) {
		return userDao.getUser(id);
	}

	@Override
	public Integer update(User user) {
		return userDao.update(user);
	}

	@Override
	public String selectUserName(String username) {
		return userDao.selectUserName(username);
	}

	@Override
	public Integer deleteUser(Integer id) {
		 
		return userDao.deleteUser(id);
	}
	
	

}
